<?php
/**
 * 创建模型类
 * 
 * 类名
 * ml_model_db...后面接自己的名字，驼峰写法
 *
 * 
 */
class ml_model_dbSysNotice extends Lib_datamodel_db 
{
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');
        parent::__construct('sys_notice' , $db_config['sys_notice']);
    }
    
    public function getListByUid ($uid , $page , $pagesize = 10)
    {
        if(!$this->init_db($uid, self::DB_SLAVE ))
        return false;
        
        $start = ($page - 1)* $pagesize;
        $sql = 'select * from '.$this->table.' where uid='.$uid.' AND status = '.self::STATUS_NORMAL.' order by ctime desc limit '.$start.' , '.$pagesize ;
        return $this->fetch($sql);
    }
    
    public function getTotalByUid ($uid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE ))
        return false;
        
        $where = ' uid='.$uid.' AND status = '.self::STATUS_NORMAL ;
        return $this->fetch_count($where);
    }
    
    public function delByUidId ($uid , $id)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;
        
        $aUpdate = array(
            'status' => self::STATUS_DELETE 
        );
        return $this->update($aUpdate , 'uid='.$uid.' AND status = '.self::STATUS_NORMAL);
    }
    
    public function addNotice($uid , $notice)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;
        
        $aData = array(
            'uid' => $uid,
            'content' => $notice,
        );
        $this->insert($aData);
    }
}
?>